//
//  LiveViewController.swift
//  Live
//
//  Created by ly on 16/5/7.
//  Copyright © 2016年 ly. All rights reserved.
//

import UIKit


class LiveViewController: UIViewController {
    // MARK:- 属性
    //将按钮名称传入
    let itmeButStr = ["常用","全部","游戏","手机游戏","鱼乐天地","颜值","鱼秀","鱼玩科技","影视发布"]
    //详情控件
    lazy var detailsScrollView : UIScrollView = UIScrollView()
    //标题控件
    lazy var titleScroll : UIScrollView = UIScrollView()
    //按钮下划线
    lazy var titleUnderline : UIView = UIView()
    
    //记录上一次点击的按钮
    var lastClickButton : UIButton?
    
    //模型
    var itmeArray : [DetailsItme]?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //
     view.backgroundColor = UIColor.whiteColor()
        //ying
        //网络请求
        
        //设置标题栏
        setuptTitleScroll()
        //添加详情页
        setupDetailsScrollView()
        //添加详情显示控制器
        setupdetailsAllController()

        //添加详情子控件
       // setupDetailsView()
    }
}

// MARK:- 添加标题控件
extension LiveViewController {
    //标题控件
    private func setuptTitleScroll() {
        //添加标题按钮UIsScrollView
        titleScroll.backgroundColor = UIColor.yellowColor()
        //设置位置
        titleScroll.frame = CGRectMake(0, statusBarH, UIScreen.mainScreen().bounds.width, titleScrollH)
        navigationItem.titleView = titleScroll
        //添加标题栏的按钮
        setupButton()
        //添加按钮的下滑线
        setupTitleUnderline()
        //默认点击第一个按钮
        let but = titleScroll.subviews[0] as! UIButton
        if but.tag == 0 {
            butClick(but)
        }
    }
    //添加标题栏的按钮
    private func setupButton() {
         var butX : CGFloat = 0
        for var i = 0; i < itmeButStr.count; i++ {
            let butStr = itmeButStr[i]
            let but = UIButton(type: .Custom)
            but.tag = i //绑定标示
            but.setTitle(butStr, forState: .Normal) //设置文字
            //设置字体大小
            but.titleLabel?.font = UIFont.systemFontOfSize(12)
            but.sizeToFit() //自适应大小
            //计算按钮的位置
            but.frame = CGRectMake(butX, 0, but.frame.width + statusBarH, titleScrollH)
            butX += but.frame.width
            but.addTarget(self, action: "butClick:", forControlEvents: .TouchUpInside)
            //文字颜色
            but.setTitleColor(UIColor.blackColor(), forState: .Normal)
            //选中状态文字颜色
            but.setTitleColor(UIColor.orangeColor(), forState: .Selected)
            titleScroll.addSubview(but)
           
        }
  
        //设置滑动范围
        titleScroll.contentSize = CGSizeMake(butX, titleScrollH)
        titleScroll.showsHorizontalScrollIndicator = false
        titleScroll.showsVerticalScrollIndicator = false
    }
    
    //添加按钮的下滑线
    private func setupTitleUnderline() {
        let firstTitleButton = titleScroll.subviews.first!
        let titleUnderlineH : CGFloat = 2
        let titleUnderlineY = titleScrollH - titleUnderlineH
        titleUnderline.frame = CGRectMake(0, titleUnderlineY, 0, titleUnderlineH)
        titleUnderline.backgroundColor = firstTitleButton.tintColor
        titleScroll.addSubview(titleUnderline)
    }
}

// MARK:- 添加详情界面
extension LiveViewController {
    //添加详情展示
    private func setupDetailsScrollView() {
        //显示详情UICollectionView 添加到底部
        view.insertSubview(detailsScrollView, atIndex: 0)
        detailsScrollView.showsVerticalScrollIndicator = false
        detailsScrollView.showsHorizontalScrollIndicator = false
        //显示范围
        detailsScrollView.frame = CGRectMake(0, 0, screenW, screenH)
        let contentW = screenW * CGFloat(itmeButStr.count)
        let contentH = screenH - titleScrollH - statusBarH
        //滑动范围
        detailsScrollView.contentSize = CGSizeMake(contentW, contentH)
        detailsScrollView.pagingEnabled = true
        detailsScrollView.delegate = self
        
    }
    
    //添加详情显示控制器
    private func setupdetailsAllController() {
        for var i = 0; i < itmeButStr.count; i++ {
            //如果是第一个则添加一个viewContriller控制器
            if i == 0 {
                let commonVC = commonViewController()
                self.addChildViewController(commonVC)
                commonVC.view.frame = CGRectMake(0, 0, screenW, screenH)
                self.detailsScrollView.addSubview(commonVC.view)
            } else {
               
                let  FiltrateVC = FiltrateViewController()
                FiltrateVC.indexController = i
                self.addChildViewController(FiltrateVC)
                //添加view
                self.detailsScrollView.addSubview(FiltrateVC.view!)
                FiltrateVC.view!.frame = CGRectMake(screenW * CGFloat(i), 0, screenW, screenH)
            }
        }
    }

}

// MARK:- 监听标题按钮点击
extension LiveViewController {
    //按钮点击调用
     func butClick(but : UIButton) {
        //取消上一次按钮的选中状态
        lastClickButton?.selected = false
        but.selected = true
        lastClickButton = but
        //改变detailsScrollView的位置
        detailsScrollView.contentOffset = CGPointMake(screenW * CGFloat(but.tag), 0)
        
        //改变下划线的位置
        //下划线的长度
        var titleUnderlineframe = titleUnderline.frame
        titleUnderlineframe.size.width = but.frame.width
        titleUnderlineframe.origin.x = but.frame.origin.x
        UIView.animateWithDuration(0.3) { () -> Void in
            self.titleUnderline.frame = titleUnderlineframe
            }
        
        //给对应的界面发送显示通知
        if but.tag != 0 {
            let filtrate =  self.childViewControllers[but.tag] as! FiltrateViewController
            filtrate.showComplete()
            
        }
        
        //将选中的按钮移到中间
        //计算出如果按钮在屏幕中央时所对应的偏移量
        let titleShouldOffsetX = but.center.x - screenW * 0.5
        let titleMaxOffsetX = titleScroll.contentSize.width - screenW
        //两头不用移动
        if titleShouldOffsetX < 0 || titleShouldOffsetX > titleMaxOffsetX {
            return
        }
        UIView.animateWithDuration(0.3) { () -> Void in
            self.titleScroll.contentOffset.x = titleShouldOffsetX
        }
     
    }
}

// MARK:- UIScrollViewDelegate
extension LiveViewController : UIScrollViewDelegate {
    //滑动停止时调用
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //计算显示第几个界面
        let contentX : Int = Int(scrollView.contentOffset.x / screenW)
        //取出对应的按钮
        let but = titleScroll.subviews[contentX] as! UIButton
        //点击对应的按钮
        butClick(but)
    }    
}

// MARK:- 网络请求
extension LiveViewController {
    
}




